package org.lmind.mis.privilege;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.TransactionAttribute;

import org.lmind.mis.core.jpa.EntityManagerQuerier;
import org.lmind.mis.core.service.BaseService;
import org.lmind.mis.privilege.internal.entity.PrivilegeEntryEntity;
import org.lmind.mis.privilege.internal.entity.PrivilegeGroupEntity;

public class PrivilegeService extends BaseService implements IPrivilegeService {

	@TransactionAttribute
	public void addPrivilegeEntries(List<PrivilegeEntry> entries) {

		for (PrivilegeEntry entry : entries) {
			PrivilegeEntryEntity entity = new PrivilegeEntryEntity();
			entity.setName(entry.getName());
			getEntityManager().merge(entity);
		}
		getEntityManager().flush();
	}

	@TransactionAttribute
	public void addPrivilegeGroups(List<PrivilegeGroup> groups) {

		for (PrivilegeGroup group : groups) {
			PrivilegeGroupEntity entity = new PrivilegeGroupEntity();
			entity.setName(group.getName());
			getEntityManager().merge(entity);
		}
		getEntityManager().flush();
	}

	@SuppressWarnings("unchecked")
	@TransactionAttribute
	public List<PrivilegeEntry> findPrivilegeEntry(PrivilegeEntryFilter filter) {

		EntityManagerQuerier querier = new EntityManagerQuerier(getEntityManager());
		String hql = 
			"SELECT pee" +
			"  FROM PrivilegeEntryEntity pee" +
			" LEFT JOIN pee.groups u";

		// 检查过滤条件
		if (filter != null) {

		}

		List<PrivilegeEntryEntity> l = (List<PrivilegeEntryEntity>)querier.query(hql);
		ArrayList<PrivilegeEntry> result = new ArrayList<PrivilegeEntry>();
		for (PrivilegeEntryEntity entity : l) {
			PrivilegeEntry bean = new PrivilegeEntry();
			bean.setId(entity.getId());
			bean.setName(entity.getName());
			result.add(bean);
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@TransactionAttribute
	public List<PrivilegeGroup> findPrivilegeGroup(PrivilegeGroupFilter filter) {

		EntityManagerQuerier querier = new EntityManagerQuerier(getEntityManager());
		String hql = 
			"SELECT pge" +
			"  FROM PrivilegeGroupEntity pge" +
			" INNER JOIN pge.users u";

		// 检查过滤条件
		Identity pi = null;
		if (filter != null) {
			pi = checkIdentity(filter.getIdentity());
			if (pi != null) {
				hql += " WHERE u.id = ";
				querier.setParameter(1, pi.getIdentity());
			}
		}

		List<PrivilegeGroupEntity> l = (List<PrivilegeGroupEntity>)querier.query(hql);
		ArrayList<PrivilegeGroup> result = new ArrayList<PrivilegeGroup>();
		for (PrivilegeGroupEntity entity : l) {
			PrivilegeGroup bean = new PrivilegeGroup();
			bean.setId(entity.getId());
			bean.setName(entity.getName());
			result.add(bean);
		}
		return result;
	}

	private Identity checkIdentity(Identity identity) {
		if (identity == null) {
			return null;
		}
		if (identity.getIdentity() == Identity.NO_IDENTITY) {
			throw new PrivilegeException("错误的PrivilegeIdentity");
		}
		return identity;
	}

	@Override
	public List<PrivilegeGroup> getPrivilegeGroup(Identity identity) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<PrivilegeGroup> getPrivilegeEntries(PrivilegeGroup group) {
		// TODO Auto-generated method stub
		return null;
	}
}
